import {queryFakeList, removeFakeList, addFakeList, updateFakeList} from '@/services/api';
import * as articleService from '../services/active'
import * as routerRedux from "react-router-redux";
import {message} from "antd";
import * as categoryService from "../services/category";

export default {
  namespace: 'active',
  state: {
    list: [],
    detail: {},
    categoryList: [],
    bookList: [],
    countNum: 500
  },

  effects: {
    * fetch({payload}, {call, put}) {
      const response = yield call(articleService.allActive, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryList',
          payload: Array.isArray(response.data) ? response.data : [],
        });
      } else {
        message.error(response.msg || "error_1")
      }
    },
    * save({payload}, {call, put}) {
      const response = yield call(articleService.saveActive, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        message.success('新增成功');
        yield put(
          routerRedux.push({
            pathname: '/active/list'
          })
        );
      } else {
        message.error(response.msg || "error_1")
      }
    },
    * detail({payload}, {call, put}) {
      const response = yield call(articleService.detailActive, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryDetail',
          payload: response.data ? response.data : {},
        });
      } else {
        message.error(response.msg || "error_1")
      }
    },
    * delete({payload}, {call, put}) {
      const response = yield call(articleService.deleteActive, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        message.success("删除成功")
        yield put({
          type: 'fetch',
          payload: {},
        });
      } else {
        message.error(response.msg || "error_1")
      }

    },
    * fetchCategory({payload}, {call, put}) {
      const response = yield call(categoryService.allCategory, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryCategory',
          payload: Array.isArray(response.data) ? response.data : [],
        });
      } else {
        message.error("获取活动类别失败！")
      }
    },
    * fetchBook({payload}, {call, put}) {
      const response = yield call(articleService.AllBook, payload);
      if(!response){
        return false;
      }
      yield put({
        type: 'queryBook',
        payload: Array.isArray(response.data) ? response.data : [],
      });
    },
    * changeActiveState({payload}, {call, put}) {
      const response = yield call(articleService.changeArticleState, payload);
      if(!response){
        return false;
      }
      const changeStatueLoading = message.loading('修改活动状态中', 0);
      setTimeout(() => {
        changeStatueLoading();
      }, 5000);
      if (response.code == "SUCCESS") {
        changeStatueLoading();
        message.success("修改成功")
        yield put({
          type: 'fetch',
          payload: {...payload.pageData, ...payload.sortData, ...payload.searchValues}
        });
      } else {
        changeStatueLoading();
        message.error(response.msg || "error_1")
      }
    },
    * queryCount({payload}, {call, put}) {
      const response = yield call(articleService.countArticle, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryCountNum',
          payload: response.data ? response.data : 0,
        });
      } else {
        message.error(response.msg || "error_1")
      }
    },
    * submit({payload}, {call, put}) {
      let callback;
      if (payload.id) {
        callback = Object.keys(payload).length === 1 ? removeFakeList : updateFakeList;
      } else {
        callback = addFakeList;
      }
      const response = yield call(callback, payload); // post
      yield put({
        type: 'queryList',
        payload: response,
      });
    },
  },
  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    queryDetail(state, action) {
      return {
        ...state,
        detail: action.payload,
      };
    },
    queryCategory(state, action) {
      return {
        ...state,
        categoryList: action.payload,
      };
    },
    queryBook(state, action) {
      return {
        ...state,
        bookList: action.payload,
      };
    },
    queryCountNum(state, action) {
      return {
        ...state,
        countNum: action.payload,
      };
    },
  }
};
